package com.huanzhidadi.singleweb.delayqueue;

import java.util.concurrent.DelayQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class DelayQueueClient {
    public static void main(String[] args) {
        ExecutorService executor = Executors.newFixedThreadPool(2);

        DelayQueue<DelayedUser> delayQueue = new DelayQueue<>();
        int messageCount = 5;
        long delayTime = 2000;

        DelayQueueProducer producer = new DelayQueueProducer(delayQueue, messageCount, delayTime, false);
        DelayQueueConsumer consumer = new DelayQueueConsumer(delayQueue, messageCount, false);

        executor.submit(producer);
        try {
            executor.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executor.submit(consumer);

        try {
            executor.awaitTermination(5, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        executor.shutdown();
    }
}
